var util = require("../util"), MiniProgramLogger = require("../miniProgramLogger"), ARMS_STORAGE_MINIPROGRAM_WX_UID_KEY = "ARMS_STORAGE_MINIPROGRAM_WX_UID_KEY", WXLogger = function(t) {
    return MiniProgramLogger.call(this, t), this;
};

WXLogger.prototype = util.createObject(MiniProgramLogger.prototype), util.ext(MiniProgramLogger._root.dftCon, {
    sendRequest: function(t, e) {
        if ("undefined" != typeof wx && wx && "function" == typeof wx.request) try {
            var r, o = "GET";
            e && (o = "POST", r = JSON.stringify(e)), wx.request({
                url: t,
                method: o,
                data: r,
                fail: function(t) {
                    util.warn("[arms] sendRequest fail", t);
                }
            });
        } catch (n) {
            util.warn("[arms] error in conf sendRequest", n);
        }
    },
    getCurrentPage: function() {
        if ("function" == typeof getCurrentPages) try {
            var t = getCurrentPages() || [], e = t[t.length - 1];
            return e && e.route || null;
        } catch (r) {
            util.warn("[arms] error in conf getCurrentPage", r);
        }
    }
}), util.ext(WXLogger.prototype, {
    constructor: WXLogger,
    _super: MiniProgramLogger,
    autoSetCommonInfo: function() {
        this.setCommonInfo({
            app: "mini_wx"
        }), this.autoSetSystemInfo(), this.autoSetNetworkType(), this.autoSetUid(), this.autoSetSid();
    },
    autoSetUid: function() {
        if (this._conf && this._conf.uid) this.setCommonInfo({
            uid: this._conf.uid
        }); else if ("undefined" != typeof wx && wx && "function" == typeof wx.getStorageSync) try {
            var t = wx.getStorageSync(ARMS_STORAGE_MINIPROGRAM_WX_UID_KEY);
            if (t && "string" == typeof t) this.setCommonInfo({
                uid: t
            }); else if ("function" == typeof wx.setStorageSync) {
                var e = util.uu();
                wx.setStorageSync(ARMS_STORAGE_MINIPROGRAM_WX_UID_KEY, e), this.setCommonInfo({
                    uid: e
                });
            }
        } catch (r) {
            util.warn("[arms] error in autoSetUid", r);
        }
    },
    autoSetSystemInfo: function() {
        if ("undefined" != typeof wx && wx && "function" == typeof wx.getSystemInfoSync) try {
            var t = wx.getSystemInfoSync();
            "object" == typeof t && this.setCommonInfo({
                sr: (t.screenWidth || 0) + "x" + (t.screenHeight || 0),
                vp: (t.windowWidth || 0) + "x" + (t.windowHeight || 0),
                dpr: t.pixelRatio,
                ul: t.language
            });
        } catch (e) {
            util.warn("[arms] error in autoSetSystemInfo", e);
        }
    },
    autoSetNetworkType: function() {
        var t = this;
        if ("undefined" != typeof wx && wx && "function" == typeof wx.getNetworkType) try {
            wx.getNetworkType({
                success: function(e) {
                    e && "string" == typeof e.networkType && t.setCommonInfo({
                        ct: e.networkType
                    });
                },
                fail: function(t) {
                    util.warn("[arms] autoSetNetworkType getNetworkType fail", t);
                }
            });
        } catch (e) {
            util.warn("[arms] error in autoSetNetworkType", e);
        }
    },
    hookApp: function(t) {
        var e = this, r = {
            onError: function(r) {
                var o = 1 === arguments.length ? [ arguments[0] ] : Array.apply(null, arguments), n = t.onError;
                try {
                    if (r && "object" == typeof r && e.error(r), r && "string" == typeof r) {
                        var i = r.split("\n"), a = "", g = "";
                        i.length > 1 && (a = i[0] && i[0].length < 100 ? i[0] : i[0].substring(0, 100), 
                        g = i[1]), e.error({
                            name: a,
                            message: g || r,
                            stack: r
                        });
                    }
                } catch (r) {
                    util.warn("[arms] error in hookApp:onError", r);
                }
                if ("function" == typeof n) return n.apply(this, o);
            },
            onLaunch: function() {
                var r = 1 === arguments.length ? [ arguments[0] ] : Array.apply(null, arguments), o = t.onLaunch;
                try {
                    e.appLaunch();
                } catch (n) {
                    util.warn("[arms] error in hookApp:onLaunch", n);
                }
                if ("function" == typeof o) return o.apply(this, r);
            }
        };
        return util.ext({}, t, r);
    }
}), require("./hook")(WXLogger);

var behavior = require("./behavior");

"undefined" != typeof wx && wx && behavior(WXLogger, wx);

var singleton = null, generator = function(t) {
    return singleton || (singleton = new WXLogger(t || {})), singleton;
};

WXLogger.createExtraInstance = function(t) {
    t && "object" == typeof t ? (t.disableHook = !0, t.behavior = !1) : t = {
        disableHook: !0,
        behavior: !1
    };
    return new WXLogger(t);
}, WXLogger.init = generator, WXLogger.singleton = generator, WXLogger._super = MiniProgramLogger, 
WXLogger._root = MiniProgramLogger._root, MiniProgramLogger.WXLogger = WXLogger, 
module.exports = WXLogger;